<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\ReportSetting;
use App\Models\DeviceType;
use App\Models\Deviceparam;
use App\Models\ReportSettingField;
use App\Http\Requests\ReportSettingRequest;
use Artisan;

class ReportSettingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $perPage = $request->input('pageSize',15);
        $name = $request->input('report_table_name', '');
        $query = ReportSetting::query();
        

        if(!empty($name)) {
            $query->where('report_table_name', 'like', $name.'%');
        }

        $query->with('settingField');

        $tables = DeviceType::select(['dt_hisdata_table', 'dt_typememo', 'dt_typeid'])->get();

        $list = $query->paginate($perPage);

        return $this->success('', [ 'data'=>$list, 'tables'=>$tables ]);
    }

    //参数列表
    public function params(Request $request)
    {
        $perPage = $request->input('pageSize',15);
        $name = $request->input('dp_paramdesc');
        $query = Deviceparam::query();

        if(!empty($name)) {
            $query->where('dp_paramdesc', 'like', '%'.$name.'%');
        }

        $typeId = $request->input('type_id', 0);

        $query->where('dt_typeid', $typeId);

        $query->where('dp_datatype', 7);

        $hasPage = $request->input('hasPage', 1);

        $hasConvert = $request->input('hasConvert', false);

        $fields = ['dp_paramname', 'dp_paramdesc'];

        if($hasConvert) {
            $fields = ['dp_paramname as prop', 'dp_paramdesc as label'];
        }

        $query->select($fields);
        $result = [];
        if($hasPage==1) {
            $list = $query->paginate($perPage);
            $result = $list;
        }else{
            $list = $query->get();
            $result = [ 'data'=>$list ];
        }

        return $result;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ReportSettingRequest $request)
    {
        $info = ReportSetting::create([
            'report_table_name'=>$request->input('report_table_name'),
            'report_table_desc'=>$request->input('dt_typememo'),
            'device_type'=>$request->input('dt_typeid'),
        ]);

        $info->settingField()->createMany($request->input('fields'));
        
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ReportSettingRequest $request, $id)
    {
        $info = ReportSetting::findOrFail($id);

        $fields = $request->input('fields', []);

        foreach($fields as $field) {
            ReportSettingField::updateOrCreate([ 'sum_field'=>$field['sum_field'], 'table_id'=>$id ], [ 'sum_desc'=>$field['sum_desc'] ]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $info = ReportSetting::findOrFail($id);
        $info->delete();
        ReportSettingField::where('table_id', $id)->delete();
    }

    //更新统计表结构
    public function updateTable(ReportSettingRequest $request, $id)
    {
        $info = ReportSetting::findOrFail($id);

        $fields = $info->settingField()
            ->select(['table_id', 'sum_field'])
            ->get()
            ->pluck('sum_field')
            ->all();
        
        $table = $info->report_table_name;

        $isForce = $request->input('force', false);


        Artisan::call('summy:table', [
            'tableName' => str_replace('t_hisdata_', '', $table),
            'fields' => $fields,
            '--force'=> $isForce
        ]);


    }


    //生成统计报表
    public function report(ReportSettingRequest $request, $table)
    {
        $test = $request->input('is_test', false);
        $isCustom = $request->input('is_custom', false);
        $date = $request->input('date');
        $shour = $request->input('shour');
        $ehour = $request->input('ehour');
        $pdi = $request->input('pdi_index');

        $inputs = [
            'table' => $table,
        ];

        if($pdi) {
            $inputs['--pdi'] = $pdi;
        }

        if($test) {
            $inputs['--test'] = $test;
        }

        if($isCustom) {
            $inputs['--shour'] = $shour;
            $inputs['--ehour'] = $ehour;
        }else{
            $inputs['date'] = $date;
        } 
        Artisan::call('sum:report', $inputs);

    }

}
